package cn.wolfcode.luowowo.util;

/*public class LoginJwtUtil {

    //秘钥
    private static  final String secret= "wolfcode";

    public static String createToken(UserInfo userInfo) {
        //私钥及加密算法
        Algorithm algorithm = Algorithm.HMAC256(secret);
        //设置头信息
        HashMap<String, Object> header = new HashMap<>(2);
        header.put("typ", "JWT");
        header.put("alg", "HS256");
        String city = "";
        String nickname = "";
        try {
            city = URLEncoder.encode(userInfo.getCity(), "UTF-8");
            nickname = URLEncoder.encode(userInfo.getNickname(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return JWT.create().withHeader(header)
                .withClaim("id", userInfo.getId())
                .withClaim("nickname", nickname)
                .withClaim("phone", userInfo.getPhone())
                .withClaim("city", city)
                .withClaim("headImgUrl", userInfo.getHeadImgUrl())

                .sign(algorithm);
    }

    public static UserInfo getUserByToken(String token) {
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secret)).build();
        UserInfo userInfo = null;
        try{
            DecodedJWT verify = jwtVerifier.verify(token);
            Map<String, Claim> claims = verify.getClaims();
            userInfo = new UserInfo();

            userInfo.setId( claims.get("id").asString());
            userInfo.setNickname(URLDecoder.decode(claims.get("nickname").asString(), "UTF-8"));
            userInfo.setPhone(claims.get("phone").asString());
            userInfo.setCity(URLDecoder.decode(claims.get("city").asString(), "UTF-8"));
            userInfo.setHeadImgUrl(claims.get("headImgUrl").asString());

        }catch (Exception e){
            e.printStackTrace();
        }
        return userInfo;
    }
}*/
